using System.Collections;
using System.Collections.Generic;
using UnityEngine;

/// <summary>
/// 摄像机的旋转
/// </summary>

public class MouseLook : MonoBehaviour
{
    [Tooltip("鼠标灵敏度")] public float mouseSensticity = 400f;
    private Transform playerBody;//玩家位置
    private float yRotation = 0f;//摄像机上下旋转的数值
    // Start is called before the first frame update
    
    private CharacterController characterController;
    [Tooltip("摄像机初始高度")] public float height = 1.8f;
    private float interpolationSpeed = 12f;//高度变化的平滑值
    void Start()
    {
        //鼠标锁定并隐藏
        Cursor.lockState = CursorLockMode.Locked;
        playerBody=transform.GetComponentInParent<PlayerController>().transform;
        characterController = GetComponentInParent<CharacterController>();
    }

    // Update is called once per frame
    void Update()
    {
        float mouseX= Input.GetAxis("Mouse X") * mouseSensticity * Time.deltaTime;
        float mouseY= Input.GetAxis("Mouse Y") * mouseSensticity * Time.deltaTime;
        yRotation -= mouseY;//将上下旋转的周值进行累计
        yRotation = Mathf.Clamp(yRotation, -60f, 60f);
        transform.localRotation = Quaternion.Euler(yRotation, 0f, 0f);//摄像机上下旋转
        playerBody.Rotate(Vector3.up * mouseX);//玩家左右移动

        //当人物下蹲或者站立时，随着高度变化，摄像机的高度也要随着变化
        float heightTarget = characterController.height * 0.9f;
        height = Mathf.Lerp(height, heightTarget, interpolationSpeed * Time.deltaTime);
        //设置下蹲时摄像机的高度
        transform.localPosition = Vector3.up * height;
    }
}
